package com.huawei.zyb.taskManager.interfaces.rest;

import com.huawei.zyb.taskManager.application.developer.DeveloperApplicationService;
import com.huawei.zyb.taskManager.application.developer.dto.DeveloperDTO;
import com.huawei.zyb.taskManager.domain.developer.valueobject.DeveloperRole;
import com.huawei.zyb.taskManager.domain.developer.valueobject.DeveloperStatus;
import com.huawei.zyb.taskManager.domain.shared.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.Email;
import java.util.List;

/**
 * 开发者控制器
 */
@Controller
public class DeveloperController {
    
    private final DeveloperApplicationService developerApplicationService;
    
    @Autowired
    public DeveloperController(DeveloperApplicationService developerApplicationService) {
        this.developerApplicationService = developerApplicationService;
    }
    
    /**
     * 开发者管理页面
     */
    @GetMapping("/developers")
    public String developersPage() {
        return "developers";
    }
    
    /**
     * 创建开发者请求对象
     */
    public static class CreateDeveloperRequest {
        @NotBlank(message = "开发者ID不能为空")
        @Size(max = 50, message = "开发者ID长度不能超过50个字符")
        private String id;
        
        @NotBlank(message = "开发者姓名不能为空")
        @Size(max = 100, message = "开发者姓名长度不能超过100个字符")
        private String name;
        
        @NotNull(message = "开发者角色不能为空")
        private DeveloperRole role;
        
        // Default constructor
        public CreateDeveloperRequest() {}
        
        // Constructor with parameters
        public CreateDeveloperRequest(String id, String name, DeveloperRole role) {
            this.id = id;
            this.name = name;
            this.role = role;
        }
        
        // Getters and Setters
        public String getId() { return id; }
        public void setId(String id) { this.id = id; }
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public DeveloperRole getRole() { return role; }
        public void setRole(DeveloperRole role) { this.role = role; }
    }
    
    /**
     * 更新开发者请求对象
     */
    public static class UpdateDeveloperRequest {
        @NotBlank(message = "开发者姓名不能为空")
        @Size(max = 100, message = "开发者姓名长度不能超过100个字符")
        private String name;
        
        @NotNull(message = "开发者角色不能为空")
        private DeveloperRole role;
        
        @NotNull(message = "开发者状态不能为空")
        private DeveloperStatus status;
        
        // Default constructor
        public UpdateDeveloperRequest() {}
        
        // Constructor with parameters
        public UpdateDeveloperRequest(String name, DeveloperRole role, DeveloperStatus status) {
            this.name = name;
            this.role = role;
            this.status = status;
        }
        
        // Getters and Setters
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public DeveloperRole getRole() { return role; }
        public void setRole(DeveloperRole role) { this.role = role; }
        public DeveloperStatus getStatus() { return status; }
        public void setStatus(DeveloperStatus status) { this.status = status; }
    }
    
    /**
     * 状态更新请求对象
     */
    public static class StatusUpdateRequest {
        @NotNull(message = "开发者状态不能为空")
        private DeveloperStatus status;
        
        // Getters and Setters
        public DeveloperStatus getStatus() { return status; }
        public void setStatus(DeveloperStatus status) { this.status = status; }
    }
    
    /**
     * 创建开发者
     */
    @PostMapping("/api/developers")
    @ResponseBody
    public ResponseEntity<?> createDeveloper(@Valid @RequestBody CreateDeveloperRequest request) {
        try {
            DeveloperDTO developer = developerApplicationService.createDeveloper(
                request.getId(),
                request.getName(),
                request.getRole()
            );
            return ResponseEntity.status(HttpStatus.CREATED).body(developer);
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 根据ID查找开发者
     */
    @GetMapping("/api/developers/{id}")
    @ResponseBody
    public ResponseEntity<?> findDeveloperById(@PathVariable String id) {
        try {
            DeveloperDTO developer = developerApplicationService.findById(id);
            return ResponseEntity.ok(developer);
        } catch (BusinessException e) {
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * 更新开发者信息
     */
    @PutMapping("/api/developers/{id}")
    @ResponseBody
    public ResponseEntity<?> updateDeveloper(@PathVariable String id, @Valid @RequestBody UpdateDeveloperRequest request) {
        try {
            // 更新开发者基本信息
            developerApplicationService.updateDeveloper(id, request.getName());
            
            // 更新角色
            developerApplicationService.changeRole(id, request.getRole());
            
            // 更新状态
            developerApplicationService.updateStatus(id, request.getStatus());
            
            DeveloperDTO updatedDeveloper = developerApplicationService.findById(id);
            return ResponseEntity.ok(updatedDeveloper);
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 删除开发者
     */
    @DeleteMapping("/api/developers/{id}")
    @ResponseBody
    public ResponseEntity<?> deleteDeveloper(@PathVariable String id) {
        try {
            developerApplicationService.deleteDeveloper(id);
            return ResponseEntity.noContent().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 更新开发者状态
     */
    @PutMapping("/api/developers/{id}/status")
    @ResponseBody
    public ResponseEntity<?> updateDeveloperStatus(@PathVariable String id, @RequestBody StatusUpdateRequest request) {
        try {
            developerApplicationService.updateStatus(id, request.getStatus());
            DeveloperDTO updatedDeveloper = developerApplicationService.findById(id);
            return ResponseEntity.ok(updatedDeveloper);
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 激活开发者
     */
    @PostMapping("/api/developers/{id}/activate")
    @ResponseBody
    public ResponseEntity<?> activateDeveloper(@PathVariable String id) {
        try {
            developerApplicationService.activateDeveloper(id);
            return ResponseEntity.ok().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 停用开发者
     */
    @PostMapping("/api/developers/{id}/deactivate")
    @ResponseBody
    public ResponseEntity<?> deactivateDeveloper(@PathVariable String id) {
        try {
            developerApplicationService.deactivateDeveloper(id);
            return ResponseEntity.ok().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    /**
     * 更改开发者角色
     */
    @PostMapping("/api/developers/{id}/role")
    @ResponseBody
    public ResponseEntity<?> changeRole(@PathVariable String id, @RequestParam DeveloperRole role) {
        try {
            developerApplicationService.changeRole(id, role);
            return ResponseEntity.ok().build();
        } catch (BusinessException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    

    

    

    
    /**
     * 根据角色查找开发者列表
     */
    @GetMapping("/api/developers/role/{role}")
    @ResponseBody
    public ResponseEntity<List<DeveloperDTO>> findDevelopersByRole(@PathVariable DeveloperRole role) {
        List<DeveloperDTO> developers = developerApplicationService.findByRole(role);
        return ResponseEntity.ok(developers);
    }
    
    /**
     * 根据状态查找开发者列表
     */
    @GetMapping("/api/developers/status/{status}")
    @ResponseBody
    public ResponseEntity<List<DeveloperDTO>> findDevelopersByStatus(@PathVariable DeveloperStatus status) {
        List<DeveloperDTO> developers = developerApplicationService.findByStatus(status);
        return ResponseEntity.ok(developers);
    }
    
    /**
     * 查找所有开发者
     */
    @GetMapping("/api/developers")
    @ResponseBody
    public ResponseEntity<List<DeveloperDTO>> findAllDevelopers() {
        List<DeveloperDTO> developers = developerApplicationService.findAll();
        return ResponseEntity.ok(developers);
    }
    
    /**
     * 根据姓名模糊查询开发者
     */
    @GetMapping("/api/developers/search")
    @ResponseBody
    public ResponseEntity<List<DeveloperDTO>> findDevelopersByNameLike(@RequestParam String name) {
        List<DeveloperDTO> developers = developerApplicationService.findByNameLike(name);
        return ResponseEntity.ok(developers);
    }
    
    /**
     * 查找可分配任务的开发者
     */
    @GetMapping("/api/developers/assignable")
    @ResponseBody
    public ResponseEntity<List<DeveloperDTO>> findAssignableDevelopers() {
        List<DeveloperDTO> developers = developerApplicationService.findAssignableDevelopers();
        return ResponseEntity.ok(developers);
    }
    
    /**
     * 查找有管理权限的开发者
     */
    @GetMapping("/api/developers/managers")
    @ResponseBody
    public ResponseEntity<List<DeveloperDTO>> findManagerialDevelopers() {
        List<DeveloperDTO> developers = developerApplicationService.findManagerialDevelopers();
        return ResponseEntity.ok(developers);
    }
}